'use strict';

var myApp = angular.module('myApp.controllers',[]);

/**
 * Controller that contains all patients and the waiting list
 */
//myApp.controller('AllPatientsCtrl', function ($scope, $http, $location, $routeParams,WaitingList) {
function AllPatientsCtrl($scope, $http, $location, $routeParams,WaitingList,PatientServices,MedecinServices) {
	
	$scope.patient = PatientServices.getGlobalPatient();
	
	$scope.getAllPatients = function(dataString) {
		var convertToArray = dataString.replace(/ +/g,',');//.split(",");
		PatientServices.getAll(convertToArray,function(data, status) {
				if (status != 200) {
					alert("Un probleme est survenu lors de la lecture des patients");
					return;
				}
				$scope.patient_list = window.GLOBAL_PATIENT_LIST = data;
				updateWorkingOn("patient-all",PatientServices,MedecinServices);
			}
		)};
	
	
	$scope.createPatient = function(here,nom,prenom,birth) {
		$scope.patient = {nom: nom, prenom: prenom, date_naissance: birth, here:here};
		$scope.patient.id = Math.floor((Math.random() * 10000));

		PatientServices.saveData($scope.patient,function(status) {
			if (status == 200) {
				$scope.patient = PatientServices.getGlobalPatient();
				window.GLOBAL_PATIENT_LIST.push($scope.patient);
				$scope.patient_list = window.GLOBAL_PATIENT_LIST;
				viewPatientDetails($scope.patient);
			}
			else {
				alert("Erreur lors de la creation du patient: "+nom+" "+prenom);
			}
		});
	};
		
	$scope.viewPatientDetails = function(patient) {
		PatientServices.setGlobalPatient({nom: patient.nom, prenom : patient.prenom, id: patient.id, path: patient.path});
		$location.url('/patient-details');
	};
	
	$scope.getPatientConsult = function(patient) {
//		$scope.patient = patient;
		PatientServices.setGlobalPatient({nom: patient.nom, prenom : patient.prenom, id: patient.id, path: patient.path});
		$scope.removePatientFromWaitingList(patient);
		$location.url('/patient-consult');
	};

	$scope.addOrRemovePatientFromWaitingList = function(patient) {
		WaitingList.addOrRemoveItem(patient);
	}
	
	$scope.removePatientFromWaitingList = function(patient) {
		patient.here = false;
		WaitingList.addOrRemoveItem(patient);
	}
	
	$scope.getWaitingList = function() {
		return WaitingList.getDataJSON();
	}

	
	$scope.removePatient = function(patientJSON) {
		var index = window.GLOBAL_PATIENT_LIST.indexOf(patientJSON);
		if (index == -1) return;
		PatientServices.deleteData(window.GLOBAL_PATIENT_LIST[index],function(status){
			if(status==200){
				if (index >= 0) {
					window.GLOBAL_PATIENT_LIST.splice(index,1);
					$scope.patient_list = window.GLOBAL_PATIENT_LIST;
				}
			}
			else alert("Probleme lors de la suppression du patient");
		});
	};
	
	$scope.getTotalPatients = function()
	{
		if (!window.GLOBAL_PATIENT_LIST) return 0;
		return window.GLOBAL_PATIENT_LIST.length;
	}

	$scope.getTotalPatientsHere = function()
	{
		if (!window.GLOBAL_PATIENT_LIST) return 0;
		return WaitingList.data.length;
	}
	
	$scope.getAllPatients("");
}
//);

/**
 * Controller that contains patient detailed information
 */

//myApp.controller('PatientDetailCtrl', );
function PatientDetailCtrl ($scope, $http, $location, $routeParams,PatientServices,MedecinServices) {
	
	$scope.savePatient = function(patient) {
		PatientServices.saveData(patient,function(status){if(status!=200) alert("An error occurred");});
	}
	
	$scope.getPatient = function() {
		if (!PatientServices.getGlobalPatient()) {
			updateWorkingOn("patient-details",PatientServices,MedecinServices);
			return;
		}
		PatientServices.readData(PatientServices.getGlobalPatient(),function(data,status){
			(status==200)?
				$scope.patient=data:
				alert("["+status+"] Une erreur s'est produite pendant la lecture du patient: "+$scope.patient.nom + " " +$scope.patient.prenom);
			updateWorkingOn("patient-detail",PatientServices,MedecinServices);
		});
	}

	$scope.getPatient();

};